package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.MatchDAO;
import com.portalEsporteClubeBahia.app.vo.ActionMatchVO;
import com.portalEsporteClubeBahia.app.vo.MatchVO;

public class MatchBO {
	
	private MatchDAO matchDAO;
	
	public MatchBO() throws ApplicationException{
		try{
			matchDAO = new MatchDAO();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<MatchVO> findAllNotFinished() throws ApplicationException{
		try{
			return matchDAO.findAllNotFinished();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<MatchVO> findAll() throws ApplicationException{
		try{
			return matchDAO.findAll();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<MatchVO> findByFilter(MatchVO filter) throws ApplicationException{
		try{
			return matchDAO.findByFilter(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<MatchVO> findByTeam(MatchVO filter) throws ApplicationException{
		try{
			return matchDAO.findByTeam(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void update(MatchVO match) throws ApplicationException{
		try{
			
			if(match.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			if(match.getChampionship().getId()==null){
				throw new ApplicationException("Missing Championship.");
			}
			if(match.getStadium().getId()==null){
				throw new ApplicationException("Missing Stadium.");
			}
			if(match.getAwayTeam().getId()==null){
				throw new ApplicationException("Missing Away Team.");
			}
			if(match.getHomeTeam().getId()==null){
				throw new ApplicationException("Missing Home Team.");
			}
			if(match.getDate()==null){
				throw new ApplicationException("Missing Date.");
			}
			if(match.getDateUTC()==null){
				throw new ApplicationException("Missing Date UTC.");
			}
			if(match.getAwayGoals()==null){
				throw new ApplicationException("Missing Away Goals.");
			}
			if(match.getHomeGoals()==null){
				throw new ApplicationException("Missing Home Goals.");
			}
			
			matchDAO.update(match);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void insert(MatchVO match) throws ApplicationException{
		try{
			
			if(match.getChampionship().getId()==null){
				throw new ApplicationException("Missing Championship.");
			}
			if(match.getStadium().getId()==null){
				throw new ApplicationException("Missing Stadium.");
			}
			if(match.getAwayTeam().getId()==null){
				throw new ApplicationException("Missing Away Team.");
			}
			if(match.getHomeTeam().getId()==null){
				throw new ApplicationException("Missing Home Team.");
			}
			if(match.getDate()==null){
				throw new ApplicationException("Missing Date.");
			}
			if(match.getDateUTC()==null){
				throw new ApplicationException("Missing Date UTC.");
			}
			
			matchDAO.insert(match);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public MatchVO findByPrimaryKey(MatchVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return matchDAO.findByPrimaryKey(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void delete(MatchVO match) throws ApplicationException{
		try{
			
			if(match.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			matchDAO.delete(match);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<ActionMatchVO> findByFilter(ActionMatchVO action) throws ApplicationException{
		try{
			return matchDAO.findByFilter(action);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<ActionMatchVO> findActionsByMatch(MatchVO matchFilter) throws ApplicationException{
		try{
			
			if(matchFilter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return matchDAO.findActionsByMatch(matchFilter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void insert(ActionMatchVO action) throws ApplicationException{
		
		try{
			
			if(action.getDescription()==null || action.getDescription().equalsIgnoreCase("")){
				throw new ApplicationException("Missing description.");
			}
			
			if(action.getMatch()==null || action.getMatch().getId()==null){
				throw new ApplicationException("Missing match.");
			}
			
			if(action.getMinutes()==null){
				throw new ApplicationException("Missing minutes.");
			}
			
			matchDAO.insert(action);
			
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void update(ActionMatchVO action) throws ApplicationException{
		
		try{
			
			if(action.getDescription()==null || action.getDescription().equalsIgnoreCase("")){
				throw new ApplicationException("Missing description.");
			}
			
			if(action.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			if(action.getMinutes()==null){
				throw new ApplicationException("Missing minutes.");
			}
			
			matchDAO.update(action);
			
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void delete(ActionMatchVO action) throws ApplicationException{
		
		try{

			if(action.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			matchDAO.delete(action);
			
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}	

}
